package cn.bdqn.layui.service.mail;

import cn.bdqn.layui.entity.vo.MailDto;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class MailService {

    @Resource
    JavaMailSender javaMailSender;

    /**
     * 发送异步邮件
     */
    @Async
    public void sendSimpleEmail(MailDto dto) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1150392623@qq.com");
        message.setTo(dto.getTos());
        message.setSubject(dto.getSubject());
        message.setText(dto.getContent());
        javaMailSender.send(message);
    }

    /**
     * 发送带html格式邮件
     * @param dto
     */
    public void sendHTMLMail(MailDto dto) {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            mimeMessageHelper.setFrom("1150392623@qq.com");
            mimeMessageHelper.setTo(dto.getTos());
            mimeMessageHelper.setSubject(dto.getSubject());
            mimeMessageHelper.setText(dto.getContent(),true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

}
